// 引入公用的自定义函数
var $ = require('../../../utils/js.js');
var img_gy_src = $.img_gy_src;

Page({
  // 页面的初始数据
  data: {
    // 图片路径的公用部分
    img_gy_src: img_gy_src,
    isOpen: false,
    global_user_id: getApp().globalData.userinfo.id,
    user_id: '',
    receive_id: '',
    statusMsg: '已断开',
    list_data: [],
    // 聊天里面的图片数组
    imgs: [],
  },
  // 生命周期函数--监听页面加载
  onLoad: function(options) {
    $.connectWss();
    this.wssInit();
    this.setData({
      user_id: options.user_id,
      receive_id: options.receive_id,
    })
    if (options.title) {
      $.title(options.title);
    }
  },
  // 生命周期函数--监听页面初次渲染完成
  onReady: function() {

  },
  // 生命周期函数--监听页面显示
  onShow: function() {
    console.log(this.data.global_user_id);

  },
  // 自定义函数   开始
  wssInit() {
    var that = this;
    // 链接失败显示
    wx.onSocketError(function(res) {
      console.log('WebSocket连接打开失败，请检查！', res);
      that.setData({
        isOpen: false,
        statusMsg: '已断开'
      });
    });
    // 监听连接成功
    wx.onSocketOpen(function(res) {
      console.log('WebSocket连接已打开！')
      that.setData({
        isOpen: true,
        statusMsg: '已连接'
      });
      wx.onSocketMessage(function(res) {
        //console.log('收到服务器内容：', res);
        var res_data = JSON.parse(res.data);
        if (res_data) {
          if (res_data.length == 1) {
            that.data.list_data.push(res_data[0]);
            console.log(that.data.list_data);

            var imgs = that.data.imgs;
            if (res_data[0].content_type == 1) {
              imgs.push(res_data[0].content);
            }

            that.setData({
              list_data: that.data.list_data,
              imgs: imgs
            })
          } else {
            var imgs = [];
            res_data.forEach((obj) => {
              if (obj.content_type == 1) {
                imgs.push(obj.content);
              }
            })
            that.setData({
              list_data: res_data,
              imgs: imgs,
            })
          }
        }
      });
      wx.sendSocketMessage({
        data: JSON.stringify({
          user_id: that.data.user_id,
          receive_id: that.data.receive_id,
        }),
        success: res => {
          //var res_data = JSON.parse(res.data); 
          console.log(res)
        },
        fail: err => {
          console.log(err)
        }
      });
    })
  },
  input_fun(e) {
    var content = $.value(e);
    this.setData({
      content: content
    })
  },
  send_msg: function() {
    if (this.data.user_id == this.data.receive_id) {
      $.ti_shi({
        title: "请不要和自己聊天！"
      });
      return false;
    }
    var that = this;
    var content = this.data.content;
    console.log(content);
    if (content) {
      wx.sendSocketMessage({
        data: JSON.stringify({
          content: content,
          user_id: that.data.user_id,
          receive_id: that.data.receive_id,
          content_type: 0,
        }),
        success: res => {
          console.log(res + '发送成功')
          that.setData({
            content: ''
          })
        },
        fail: err => {
          console.log(err + '失败')
        }
      });
    }
  },
  // 上传图片
  add_img() {
    var that = this;
    $.add_img(1, function(res) {
      console.log("图片", res.data.data.url);
      wx.sendSocketMessage({
        data: JSON.stringify({
          content: res.data.data.url,
          user_id: that.data.user_id,
          receive_id: that.data.receive_id,
          content_type: 1,
        }),
        success: res => {
          console.log(res + '发送成功')
          that.setData({
            content: ''
          })
        },
        fail: err => {
          console.log(err + '失败')
        }
      });
      // 成功回调
      res.success();
    });
  },
  // 预览图片
  yu_lan_fun(e) {
    var img = $.data(e, "img");
    $.yu_lan(img, this.data.imgs);
  },
  // 自定义函数   结束
  // 生命周期函数--监听页面隐藏
  onHide: function() {
    wx.onSocketOpen(function() {
      wx.closeSocket()
    })
    wx.onSocketClose(function(res) {
      console.log('WebSocket 已关闭！')
    })
  },
  // 生命周期函数--监听页面卸载
  onUnload: function() {
    //console.log('监听页面卸载');
    wx.closeSocket();
  },
  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: function() {

  },
  // 页面上拉触底事件的处理函数
  onReachBottom: function() {

  },
  // 用户点击右上角分享
  // onShareAppMessage: function() {

  // }
})